import React, { Component } from 'react'
import store from '../../redux/store'

export default class Count extends Component {
	state = {username:'老刘'}

	/* componentDidMount(){
		//监测redux中数据的修改
		store.subscribe(()=>{
			this.forceUpdate()
		})
	} */

	render() {
		return (
			<div>
				<h2>当前求和为：{store.getState()}</h2>
				<h4>我最爱的男人是:{this.state.username}</h4>
				<select ref={ c => this.selectNumber = c}>
					<option value="1">1</option>
					<option value="2">2</option>
					<option value="3">3</option>
				</select>&nbsp;
				<button onClick={this.increment}>+</button>&nbsp;
				<button onClick={this.decrement}>-</button>&nbsp;
				<button onClick={this.incrementIfOdd}>当前和为奇数再+</button>&nbsp;
				<button onClick={this.incrementWait}>等一等再加</button>
			</div>
		)
	}

	//加法
	increment = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		//分发一个加的action
		store.dispatch({type:'increment',data:value*1})
	}

	//减法
	decrement = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		//分发一个减的action
		store.dispatch({type:'decrement',data:value*1})
	}

	//奇数再加
	incrementIfOdd = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		if(store.getState() % 2){
			store.dispatch({type:'increment',data:value*1})
		}
	}

	//等一等再加
	incrementWait = ()=>{
		//获取用户的输入
		const {value} = this.selectNumber
		//更新状态
		setTimeout(()=>{
			store.dispatch({type:'increment',data:value*1})
		},500)
	}
}
